package controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import pojo.Notice;
import pojo.User;
import service.NoticeService;

@Controller
public class NoticeController {

	@Autowired
	private NoticeService noticeService;

	@RequestMapping("/toaddnotice")
	public String toAddNotice(HttpSession httpSession, Model model) {
		model.addAttribute("mainPage", "noticeadd.jsp");
		return "admin/main";
	}

	@RequestMapping("/addNotice")
	public String addNotice(HttpSession httpSession, Notice notice) {
		User user1 = (User) httpSession.getAttribute("loginUser");
		Integer user_id = user1.getId();
		notice.setN_crtTime(new Date());
		notice.setN_crtUser(user_id);
		noticeService.add(notice);
		return "redirect:findNoticelist.action";
	}

	@RequestMapping("/toupdateNotice")
	public String toupdateNotice(Integer n_id, Model model) {
		Notice notice = noticeService.findById(n_id);
		model.addAttribute("notice", notice);
		model.addAttribute("mainPage", "noticeupdate.jsp");
		return "admin/main";
	}

	@RequestMapping("/updateNotice")
	public String updateNotice(HttpSession httpSession, Notice notice) {
		noticeService.update(notice);
		return "redirect:findNoticelist.action";
	}

	@RequestMapping("/findNoticelist")
	public String findNoticelist(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Map<String, Object>> notices = noticeService.findNoticelist();
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(notices);
		model.addAttribute("notices", notices);
		model.addAttribute("p", p);
		model.addAttribute("mainPage", "noticelist.jsp");
		return "admin/main";
	}

	@RequestMapping("/findNoticeFD")
	public String findNoticeFD(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Map<String, Object>> notices = noticeService.findNoticelist();
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(notices);
		model.addAttribute("notices", notices);
		model.addAttribute("p", p);
		model.addAttribute("mainPage", "notices.jsp");
		return "fangdong/main";
	}

	@RequestMapping("/findNoticeZK")
	public String findNoticeZK(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Map<String, Object>> notices = noticeService.findNoticelist();
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(notices);
		model.addAttribute("notices", notices);
		model.addAttribute("p", p);
		model.addAttribute("mainPage", "notices.jsp");
		return "zuke/main";
	}

	@RequestMapping("/delNotice")
	public String delNotice(HttpSession httpSession, Integer n_id) {
		noticeService.delById(n_id);
		return "redirect:findNoticelist.action";
	}

}
